home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 43.zip / Sources C - WorkDisk II.adf / scales.c < prev    next >
C/C++ Source or Header  |  1987-01-02  |  18KB  |  608 lines

  1. /* SCALES
  2.  *
  3.  *   by Steven A. Bennett
  4.  *
  5.  *   This program demonstrates the use of the Audio functions
  6.  * in the "ROM" to produce four voice sound.  It uses a simple
  7.  * waveform (sawtooth) with no amplitude control (ie, envelope)
  8.  * or frequency variation (ie, vibrato), but these can easily be
  9.  * implemented through the use of setpv(), below.
  10.  * This program may be freely distributed.
  11.  *
  12.  * NOTES
  13.  *
  14.  *   - This program was written fairly quickly, without the aid
  15.  * of the ROM Kernal Manual (save about 10 pages of notes copied
  16.  * from my dealer's copy).  Therefore, if anyone sees anything too
  17.  * much out of the ordinary, bear with it.  (Particularly in mind
  18.  * is CreatePort(), of whose use I inferred from several programs
  19.  * I have downloaded from both BIX and CompuServe)  Furthermore, this
  20.  * particular implementation was written with a full blown music
  21.  * driver in mind, therefore, some code may seem ineffecient, for
  22.  * the sake of speed.  Finally, I tend to get a bit sloppy when i'm
  23.  * working past midnight, so there may be some messy areas.
  24.  *
  25.  * REVISIONS
  26.  *
  27.  *   12/25/85 - Single voice driver written. (SAB)
  28.  *   12/26/85 - Multi-voice capability added. (SAB)
  29.  *   12/28/85 - Error handling refined (see FinishProg()) (SAB)
  30.  *   12/30/85 - Fixed cleanup routine. (SAB)
  31.  *
  32.  */
  33.  
  34. #include "exec/types.h"
  35. #include "exec/memory.h"
  36. #include "devices/audio.h"
  37. #include "stdio.h"
  38.  
  39. #define  PRIORITY          10    /* Priority for Audio Channel usage */
  40. #define  NBR_IOA_STRUCTS   10    /* Number of IOAudio structures used */
  41. #define  PV_IOA_STRUCT     0     /* index to ioapv struct */
  42. #define  FIN_IOA_STRUCT    9     /* index to finishioa struct */
  43. #define  BIG_WAVE          256   /* size of biggest waveform */
  44. #define  NBR_WAVES         7     /* number of waves per instrument */
  45. #define  WAVES_TOTAL       1024  /* alloc size for instrument's waves */
  46. #define  YES               1
  47. #define  NO                0
  48.  
  49. extern struct MsgPort *CreatePort();
  50.  
  51. UBYTE aMap[] = { 0x0f };                  /* allocate four channels */
  52. UBYTE voiceMap[] = { 1, 2, 4, 8 };
  53. struct IOAudio *ioa, *finishioa, *ioapv,music;
  54. struct IOAudio *ioainuse[4];
  55. struct IOAudio *freeioa[4];
  56. int unitno = 1;
  57. int error;
  58. int waiting[4] = { NO, NO, NO, NO };
  59. int woffsets[] =
  60.    { 0, 256, 384, 448, 480, 496, 504, 508, 510 };
  61. int wlen[] =
  62.    { 256, 128, 64, 32, 16, 8, 4, 2, 1 };
  63. int perval[] =
  64.    { 428, 404, 381, 360, 339, 320, 302, 285, 269, 254, 240, 226, 214 };
  65. BYTE *wptr;
  66. BYTE *owptr[4] = { NULL, NULL, NULL, NULL };
  67.  
  68. char *portstring[] = {
  69.    "Audio one",
  70.    "Audio two",
  71.    "Audio three",
  72.    "Audio four",
  73.    "Audio five",
  74.    "Audio six",
  75.    "Audio seven",
  76.    "Audio eight" };              /* names for the CreatePorts */
  77.  
  78. /* InitIOA()
  79.  *   This function initializes all IOAudio structures used by this
  80.  * program.
  81.  *
  82.  * NOTES
  83.  *   This version sets up enough IOAudio structures for four voices, plus
  84.  * a ADCMD_FINISH and an ADCMD_PERVOL, which are both synchronous.
  85.  * The ioainuse array is assumed to be the IOAudio structure which is
  86.  * being used by the current CMD_WRITE command for each voice.  The freeioa
  87.  * array is there so that no <click> is made when switching CMD_WRITEs.
  88.  * (ie., for speed)  Since the pointers are swapped when the switch occurs,
  89.  * one can always make the same assumption.  Note that ALL of the
  90.  * asynchronous (ie., ioainuse and freeioa) structures ought to have a
  91.  * unique ReplyPort (or so I think).  The IOAudio structure used to
  92.  * Open the Device must also have a ReplyPort, but it may not have to be
  93.  * unique.  I am taking no chances, however.
  94.  */
  95.  
  96. InitIOA()
  97.    {
  98.    int i;
  99.  
  100.    /* alloc the IOAudio structures
  101.     */
  102.    ioa = (struct IOAudio *)AllocMem((NBR_IOA_STRUCTS * sizeof(*ioa)),
  103.       MEMF_PUBLIC | MEMF_CLEAR);
  104.    if (ioa == NULL)
  105.       FinishProg(1);
  106.  
  107.    /* set the various IOAudio structure pointers
  108.     */
  109.    for (i = 0; i < 4; ++i)
  110.       {
  111.       ioainuse[i] = &ioa[i + 1];
  112.       freeioa[i] = &ioa[i + 5];
  113.       }
  114.    ioapv = &ioa[PV_IOA_STRUCT];
  115.    finishioa = &ioa[FIN_IOA_STRUCT];
  116.  
  117.    /* Open the Audio Device.  This requires a ReplyPort, so we'll
  118.     * make it now, and reuse it later as part of ioapv, although it
  119.     * doesn't need one.  aMap[] is an array containing sets of
  120.     * bitmaps which allow various combinations of voices to be allocated.
  121.     * since we want all four voices, there is only one combination which
  122.     * will serve.
  123.     */
  124.    ioa->ioa_Request.io_Message.mn_Node.ln_Pri = PRIORITY;
  125.    ioa->ioa_Request.io_Message.mn_ReplyPort =
  126.       CreatePort("Audio zero", 0);
  127.    if (ioa->ioa_Request.io_Message.mn_ReplyPort == NULL)
  128.       FinishProg(2);
  129.    ioa->ioa_Data = aMap;
  130.    ioa->ioa_Length = sizeof(aMap);
  131.    error = OpenDevice(AUDIONAME, 0, ioa, 0);
  132.    if (error)
  133.       FinishProg(3);
  134.  
  135.    /* setup the finishioa and ioapv structs.  The IOF_QUICK flag
  136.     * makes them synchronous in all cases.  
  137.     */
  138.    *finishioa = *ioa;
  139.    finishioa->ioa_Request.io_Flags =
  140.        ioapv->ioa_Request.io_Flags = IOF_QUICK;
  141.  
  142.    finishioa->ioa_Request.io_Command = ADCMD_FINISH;
  143.    ioapv->ioa_Request.io_Command = ADCMD_PERVOL;
  144.  
  145.    /* setup the ioainuse and freeioa struct arrays.  All eight of
  146.     * them must have unique ReplyPorts, hence the CreatePort()s
  147.     * below.  portstring contains different port names for the
  148.     * eight ports.  (I have no idea as to what the zero in the
  149.     * CreatePort is used for, so don't ask.)
  150.     */
  151.    for (i = 0; i < 4; ++i)
  152.       {
  153.       music=*ioa; 
  154.       *ioainuse[i] = music;
  155.       *freeioa[i] = *ioainuse[i];
  156.  
  157.       freeioa[i]->ioa_Request.io_Message.mn_ReplyPort =
  158.          CreatePort(portstring[i], 0);
  159.       ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort =
  160.          CreatePort(portstring[i + 4], 0);
  161.       }
  162.    for (i = 0; i < 4; ++i)
  163.       if (freeioa[i]->ioa_Request.io_Message.mn_ReplyPort == NULL ||
  164.          ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort == NULL)
  165.          FinishProg(4);
  166.    }
  167.  
  168. /* FinishProg(finishcode)
  169.  *    int finishcode;
  170.  *
  171.  *    displays an error message, if necessary, based on the value
  172.  * of finishcode, frees all allocated stuff (again based on finishcode)
  173.  * and then exits.
  174.  *
  175.  * NOTES
  176.  *   This is basically a tidy little "clean up" routine used by all exit
  177.  * routines in this program.  Since all the allocated "stuff" is supposed
  178.  * to be in global variables, freeing it up in one global location is
  179.  * much easier then doing it elsewhere.
  180.  *
  181.  */
  182.  
  183. char *errormsgs[] = {
  184.    "Finished!\n",
  185.    "Cannot allocate memory for IOAudio structures\n",
  186.    "Cannot create ReplyPort for OpenDevice call\n",
  187.    "Cannot open Audio Device\n",
  188.    "Cannot create ReplyPort(s) for remaining IOAudio structures\n",
  189.    "Cannot allocate memory for waveform\n",
  190.    "If you see this during execution, execute the programmer.\n" };
  191.  
  192. FinishProg(finishcode)
  193.    int finishcode;
  194.    {
  195.    int i;
  196.  
  197.    printf(errormsgs[finishcode]);
  198.    switch(finishcode)
  199.       {
  200.    case 0:
  201.       /* free up the WaveNode list
  202.        * (currently, just wptr)
  203.        */
  204.       FreeMem(wptr, WAVES_TOTAL);
  205.  
  206.    case 4:
  207.    case 5:
  208.       /* free up all ReplyPorts save the first.  Since we could be
  209.        * here due to an error in allocating same, we check first.
  210.        * (And yes, Virginia, we do fall through here)
  211.        */
  212.       for (i = 0; i < 4; ++i)
  213.          {
  214.          if (freeioa[i]->ioa_Request.io_Message.mn_ReplyPort)
  215.             DeletePort(freeioa[i]->ioa_Request.io_Message.mn_ReplyPort);
  216.          if (ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort)
  217.             DeletePort(ioainuse[i]->ioa_Request.io_Message.mn_ReplyPort);
  218.          }
  219.  
  220.       /* Close the Audio Device
  221.        */
  222.       CloseDevice(ioa);
  223.  
  224.    case 3:
  225.       /* Delete the first ReplyPort
  226.        */
  227.       DeletePort(ioa->ioa_Request.io_Message.mn_ReplyPort);
  228.  
  229.    case 2:
  230.       /* Free the ioa memory
  231.        */
  232.       FreeMem(ioa, (NBR_IOA_STRUCTS * sizeof(*ioa)));
  233.  
  234.       }
  235.    if (finishcode)
  236.       exit(1);
  237.    exit(0);
  238.    }
  239.  
  240. /* setwpv()
  241.  *
  242.  *   starts a sound on the channel specified by the global variable
  243.  * unitno.  By swapping between two IOAudio structures, this routine
  244.  * is able to accomplish it's task without a noticeable delay.
  245.  *
  246.  * NOTES
  247.  *   This routine should only be used when a waveform change is
  248.  * required, either due to frequency going out of the range of the
  249.  * existing waveform, or a different waveform has been chosen.  In
  250.  * most cases, setpv() will be sufficient.  Also, don't complain about
  251.  * the usage of io_Unit.  I didn't like it either.
  252.  *
  253.  */
  254.  
  255. setwpv(wf, len, per, vol, voice)
  256.    char *wf;
  257.    int len, per, vol, voice;
  258.    {
  259.    struct IOAudio *tmpioa;
  260.  
  261.    /* the next three lines are probably unnecessary and can be
  262.     * done instead in InitIOA, but why take chances?
  263.     */
  264.    freeioa[voice]->ioa_Request.io_Command = CMD_WRITE;
  265.    freeioa[voice]->ioa_Request.io_Flags = ADIOF_PERVOL | IOF_QUICK;
  266.    freeioa[voice]->ioa_Cycles = 0;
  267.  
  268.    /* Assign the unit numbers to the (<ahem>) pointer?
  269.     */
  270.    freeioa[voice]->ioa_Request.io_Unit = unitno;
  271.    finishioa->ioa_Request.io_Unit = unitno;
  272.  
  273.    /* Set the parameters
  274.     */
  275.    freeioa[voice]->ioa_Data = wf;
  276.    freeioa[voice]->ioa_Length = len;
  277.    freeioa[voice]->ioa_Period = per;
  278.    freeioa[voice]->ioa_Volume = vol;
  279.  
  280.    /* Terminate the old request, if there is one.  waiting[] is an
  281.     * boolean (well, pseudo-boolean) array stating if there was an
  282.     * old request.  The old CMD_WRITE must be finished, and the reply
  283.     * received, before the new CMD_WRITE will work.  I am not certain
  284.     * why this is necessary.  Originally, I tried a
  285.     *               BeginIO(freeioa[voice]);
  286.     *               BeginIO(finishioa);
  287.     *               WaitIO(ioainuse[voice]);
  288.     * series, but the net effect was that the freeioa request was
  289.     * ignored (without error), and the ioainuse request continued
  290.     * blindly onwards.  I had assumed that it would act as a FIFO queue,
  291.     * but apparently not.
  292.     */
  293.    if (waiting[voice])
  294.       {
  295.       BeginIO(finishioa);
  296.       WaitIO(ioainuse[voice]);
  297.       waiting[voice] = NO;
  298.       }
  299.  
  300.    /* now start up the new voice
  301.     */
  302.    BeginIO(freeioa[voice]);
  303.    error = CheckIO(freeioa[voice]);
  304.    if (error)
  305.       {
  306.       printf("Error on CMD_WRITE\n");
  307.       WaitIO(freeioa[voice]);
  308.       }
  309.    waiting[voice] = YES;
  310.  
  311.    /* swap the pointers.  That way, the next time we pass through, we
  312.     * will still work.
  313.     */
  314.    tmpioa = ioainuse[voice];
  315.    ioainuse[voice] = freeioa[voice];
  316.    freeioa[voice] = tmpioa;
  317.    }
  318.  
  319. /* setpv(per, vol)
  320.  *   int per, vol;
  321.  *
  322.  *   Changes the period and volume of the currently executing
  323.  * CMD_WRITE on the specified unitno.
  324.  *
  325.  * NOTES
  326.  *   This routine is perfect <ahem> as it is, both for single and
  327.  * multiple voices.  It can be used to change frequency, within limits,
  328.  * change volume, and simulate vibrato and envelope controls.
  329.  * For those of you wondering where I got my period values
  330.  * from, look in the ABASIC manual, page 138.  Or, if you wish, you
  331.  * can calculate it out for yourself using the formula:
  332.  *     period = C/(ns*hz)
  333.  *       - where C is the clock rate (3579545)
  334.  *               ns is the number of samples in a cycle of the wave
  335.  *               hz is the number of cycles per second.
  336.  * Thus, for middle A on the piano (440 hz) with 32 samples in the
  337.  * cycle of the waveform, one would get:
  338.  *     period = 3579545 / (32 * 440)
  339.  *            = 3579545 / 14080
  340.  *            = 254.229 (or pretty close)
  341.  * The period must be rounded to the nearest integer, which can result
  342.  * in a maximum frequency error of about .25%, assuming one uses the
  343.  * octave for frequency between period 226 and period 428.  (This comes
  344.  * out to be less than a twentieth step at the shortest period)
  345.  * period values of less than 127 are illegal, as there aren't enough
  346.  * cycles set aside for audio DMA for anything less.  period values of
  347.  * greater than 500 or so aren't recommended as the anti-aliasing
  348.  * filter isn't of much use then, and actually could cause a possible
  349.  * high pitched overtone, which I'm sure nobody wants.  Thus I am
  350.  * only going to use this to handle a single octave's range.
  351.  * Changes of octave are accomplished by doubling or halving the number
  352.  * of samples in one cycle of the waveform, and must, therefore, call
  353.  * setwpv().
  354.  */
  355.  
  356. setpv(per, vol)
  357.    int per, vol;
  358.    {
  359.    ioapv->ioa_Period = per;
  360.    ioapv->ioa_Volume = vol;
  361.    ioapv->ioa_Request.io_Unit = unitno;
  362.    BeginIO(ioapv);
  363.    }
  364.  
  365. /* StopVoices()
  366.  *
  367.  *   Terminates all CMD_WRITE routines in progress, effectively
  368.  * ending all sound.  Volume is set to zero first, just to be tidy.
  369.  *
  370.  */
  371.  
  372. StopVoices()
  373.    {
  374.    int voice;
  375.  
  376.    for (voice = 0; voice < 4; ++voice)
  377.       {
  378.       if (waiting[voice])
  379.          {
  380.          /* to stop a voice, we first set it's volume to zero
  381.           * (probably unnecessary) and then finish the CMD_WRITE.
  382.           */
  383.          unitno = voiceMap[voice];
  384.          setpv(128, 0);
  385.          finishioa->ioa_Request.io_Unit = unitno;
  386.          BeginIO(finishioa);
  387.          WaitIO(ioainuse[voice]);
  388.          waiting[voice] = NO;
  389.          }
  390.       }
  391.    }
  392.  
  393. /* setwave(wfp)
  394.  *   BYTE wfp;
  395.  *
  396.  *   this routine makes the first 256 bytes (lowest octave) of the
  397.  * sawtooth wave's waveform table.  wfp must have already been
  398.  * allocated and in CHIP MEMORY!!!  This is necessary later for the
  399.  * CMD_WRITE commands to work.
  400.  *   a sawtooth wave is a simple waveform, very easy to use.  It
  401.  * basically looks like this:
  402.  *
  403.  *     /|      /|      /|      /|
  404.  *    / |     / |     / |     / |
  405.  *   /  |    /  |    /  |    /  |
  406.  *  /   |   /   |   /   |   /   |
  407.  *      |  /    |  /    |  /    |  /
  408.  *      | /     | /     | /     | /
  409.  *      |/      |/      |/      |/
  410.  *
  411.  */
  412.  
  413. setwave(wfp)
  414.    UBYTE *wfp;          /* This is a sneaky way of making a sawtooth */
  415.    {
  416.    int i;
  417.  
  418.    for (i = 0; i < BIG_WAVE; ++i)
  419.       wfp[i] = i;
  420.    }
  421.  
  422. /* xpandwave(wfp)
  423.  *   BYTE *wfp;
  424.  *
  425.  * xpandwave expands a wave with only the base BIG_WAVE specified,
  426.  * into a set of NBR_WAVES waveforms, each for one octave.
  427.  * All of the waveforms are left in contiguous memory after the
  428.  * first wave, in order of decending sizes (256, 128, 64, ...)
  429.  *
  430.  * NOTES
  431.  *   Be forewarned that this function makes two assumptions.  The
  432.  * first is that there is enough memory in the wfp buffer to hold the
  433.  * expansion, and two, that the stuff is in chip mem.  (Actually,
  434.  * makewaves() makes that assumption instead.)  This function ought to
  435.  * work for any BIG_WAVE which is a power of two greater than 2^NBR_WAVES
  436.  *
  437.  */
  438.  
  439. xpandwave(wfp)
  440.    BYTE *wfp;
  441.    {
  442.    int i, j, rate;
  443.    BYTE *tptr;
  444.  
  445.    rate = 1;
  446.    tptr = wfp + BIG_WAVE;
  447.    for (i = 0; i < NBR_WAVES - 1; ++i)
  448.       {
  449.       rate *= 2;
  450.       for (j = 0; j < BIG_WAVE; j += rate)
  451.          *tptr++ = wfp[j];
  452.       }
  453.    }
  454.  
  455. /* makewaves()
  456.  *
  457.  *   just makes a sawtooth waveform in chip mem and expands it without
  458.  * the pretty list control and file IO stuff.
  459.  *
  460.  */
  461.  
  462. makewaves()
  463.    {
  464.    /* allocate the memory for the waveform.
  465.     */
  466.    wptr = (BYTE *)AllocMem(WAVES_TOTAL, MEMF_CHIP);
  467.    if (wptr == NULL)
  468.       FinishProg(5);
  469.  
  470.    /* get and expand the waveform
  471.     */
  472.    setwave(wptr);
  473.    xpandwave(wptr);
  474.    }
  475.  
  476. /* strike(note, voice)
  477.  *   int note, voice;
  478.  *
  479.  * This nice little routine takes a note and plays it on the given
  480.  * voice.  At a fixed amplitude.  The note is basically an integer from
  481.  * 0 to 11 (c to b) plus 12 per octave above the first and lowest.  It
  482.  * uses a pointer to the last waveform used by the voice specified to
  483.  * determine if it needs to call setwpv() or just setpv().  The waveform
  484.  * is used by adding an index (woffsets[]) dependant on the octave.
  485.  * the length of the waveform (in wlen[]) is likewise dependant on
  486.  * the octave.  Note that octaves start with zero, not one.
  487.  */
  488.  
  489. strike(note, voice)
  490.    int note, voice;
  491.    {
  492.    int per, oct, len;
  493.    BYTE *wfp;
  494.  
  495.    unitno = voiceMap[voice];
  496.    if (note >= 100)           /* play a rest. */
  497.       {
  498.       if (waiting[voice])
  499.          setpv(200, 0);
  500.       return;
  501.       }
  502.    oct = note / 12;
  503.    per = perval[note % 12];
  504.    wfp = wptr + woffsets[oct];
  505.  
  506.    /* if the waveform hasn't changed since the last strike,
  507.     * then only change the period.
  508.     */
  509.    if (wfp == owptr[voice])
  510.       setpv(per, 32);         /* fixed volume */
  511.    else
  512.       {
  513.       setwpv(wfp, wlen[oct], per, 32, voice);
  514.       owptr[voice] = wfp;
  515.       }
  516.    }
  517.  
  518. main()
  519.    {
  520.    int i, j;
  521.  
  522.    InitIOA();
  523.    makewaves();
  524.  
  525.    /* simple scale
  526.     */
  527.    for (i = 0; i < 24; ++i)
  528.       {
  529.       strike(i, 0);
  530.       Delay(10);
  531.       }
  532.  
  533.    /* crossed scales
  534.     */
  535.    for (j = 48; i < 48; ++i, --j)
  536.       {
  537.       strike(i, 0);
  538.       strike(j, 1);
  539.       Delay(10);
  540.       }
  541.  
  542.    /* crossed scales with lower in sync
  543.     */
  544.    for (j = 48; i < 72; ++i, --j)
  545.       {
  546.       strike(i, 0);
  547.       strike(i - 48, 1);
  548.       strike(j, 2);
  549.       Delay(10);
  550.       }
  551.   /* crossed scales with lower in sync
  552.     */
  553.    for (j = 0; i >= 72; --i, ++j)
  554.       {
  555.       strike(i, 0);
  556.       strike(i - 48, 1);
  557.       strike(j, 2);
  558.       Delay(10);
  559.       }
  560.  
  561.    /* slow c chord...
  562.     */
  563.    strike(36, 0);
  564.    strike(100, 1);
  565.    strike(100, 2);
  566.    Delay(60);
  567.    strike(40, 1);
  568.    Delay(60);
  569.    strike(43, 2);
  570.    Delay(120);
  571.  
  572.    /* ...and into a minor with the fourth voice
  573.     */
  574.    strike(46, 3);
  575.    Delay(200);
  576.  
  577.    /* and two quick c chords to finish it up
  578.     */
  579.    restall();
  580.    Delay(20);
  581.    cchord();
  582.    Delay(20);
  583.    restall();
  584.    Delay(1);
  585.    cchord();
  586.    Delay(200);
  587.    StopVoices();
  588.    FinishProg(0);
  589.    }
  590.  
  591. restall()
  592.    {         
  593.    int i;
  594.  
  595.    for (i = 0; i < 4; ++i)
  596.       strike(100, i);
  597.    }
  598.  
  599. cchord()
  600.    {
  601.    strike(36, 0);
  602.    strike(40, 1);
  603.    strike(43, 2);
  604.    strike(48, 3);
  605.    }
  606.  
  607.  
  608.